package org.qloudgen.txn.dilithium.cluster.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.txn.dilithium.cluster.beans.ParameterBean;
import org.qloudgen.txn.dilithium.cluster.common.CommonValues;
import org.qloudgen.txn.dilithium.cluster.database.ClusterImpl;
import org.qloudgen.txn.dilithium.cluster.pages.common.BasePage;

public class ShowConfPage extends BasePage{
	
	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */

	static Log log = LogFactory.getLog(ShowConfPage.class);
	
	private static final long serialVersionUID = 1L;
	private transient List<ParameterBean> paramList ;
	
	@SuppressWarnings({"rawtypes","unchecked"})
    public ShowConfPage(PageParameters pageParameters){
		
		super();

		this.pp = pageParameters;
		
		final int clusterID = pageParameters.get("clusterID").toInteger();
		
		final WebMarkupContainer paramBorder = new WebMarkupContainer("paramBorder");
		paramBorder.setOutputMarkupId(true);
		
		final CheckGroup<ParameterBean> paramGroup = new CheckGroup<ParameterBean>("paramGroup", new ArrayList<ParameterBean>());
		paramGroup.setRenderBodyOnly(false);
		paramGroup.setOutputMarkupId(true);
		paramBorder.add(paramGroup);
		
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		this.add(feedback);
				
		//get conf from metadb
		paramList = ClusterImpl.getClusterConf(clusterID);
		
		final Form form = new Form("form") ;
		this.add(form);
		form.add(paramBorder);
		
		final PageableListView<ParameterBean> paramListView = new PageableListView<ParameterBean>( "tableRows", paramList,CommonValues.CLUSTER_CONF_PARAM_LIST) {
			
			private static final long serialVersionUID = 1L;
			protected void populateItem(ListItem<ParameterBean> item) {
				
				final ParameterBean param=(ParameterBean) item.getModelObject();
				item.add(new Label( "paramType", param.getParamType()));
				item.add(new Label( "chnName", param.getChnName()));
				TextField paramValue = new TextField( "paramValue");
				paramValue.setDefaultModel(new Model(String.valueOf(param.getParamValue())));
				paramValue.setEnabled(false);
				item.add(paramValue);
				
			}
		};
		paramGroup.add(paramListView);
		
		Button cancel = new Button ("cancel"){
			private static final long serialVersionUID = 1L;
			public void onSubmit() {
				setResponsePage(ClusterMainPage.class,pp);
			}
		};
		
		form.add(cancel);
		cancel.setDefaultFormProcessing(false);
		
		this.addFooter();
	}
}